import dash
from dash import Dash
import feffery_antd_components as fac
from gypb.config import settings
from gypb.initdb import initilize_db
import os
import rqdatac

def get_fac_menu(pages=False):
    submenus = {
        "data": [],
        "sale": [],
    }
    exclude = ["project_detail"]
    menuItems = [
            {
                "component": "SubMenu",
                "props": {
                    "key": "sale",
                    "title": "代销",
                },
                "children": submenus["sale"]
            },
            {
                "component": "SubMenu",
                "props": {
                    "key": "data",
                    "title": "数据管理",
                },
                "children": submenus["data"]
            },
            
    ]
    # print(len(dash.page_registry))
    for page in dash.page_registry.values():
        if pages:
            if not (page["name"] in pages):
                continue
        path = page["relative_path"].split("/")
        if len(path) == 2:
            # 直接在根菜单
            menuItems.append(
                {
                    "component": "Item",
                    "props": {
                        "key": page["name"],
                        "title": page["name"],
                        "href": page["path"],
                    },
                }
            )
        else:
            level = path[1]
            menu_list = submenus.get(level)
            if menu_list is not None:
                if page["name"] not in exclude:
                    menu_list.append(
                        {
                            "component": "Item",
                            "props": {
                                "key": page["name"],
                                "title": page["name"],
                                "href": page["path"],
                            },
                        }
                )
    return fac.AntdMenu(
        menuItems=menuItems,
        mode="inline",
        # theme="dark",
        style={"width": 256},
    )

pages = settings.pages

rqdatac.init(settings.rqdata.user,settings.rqdata.password)

# 初始化数据库
initilize_db()

# 创建 Dash 应用
app = Dash(
    __name__,
    use_pages=True,
    suppress_callback_exceptions=True,
)
dash.register_page("home", layout="首页", path="/", order=0, name="主页")
# print(dash.page_registry)
app.layout = fac.AntdLayout(
    [
        # fac.AntdHeader(
        #     navbar,
        # ),
        fac.AntdLayout(
            [
                fac.AntdSider(
                    fac.AntdCenter(
                        style={
                            "width": "200px",
                            "backgroundColor": "#f8f9fa",
                            "padding": "10px",
                        },
                        children=[get_fac_menu(pages)],
                    ),
                    style={
                        "backgroundColor": "rgb(240, 242, 245)",
                        "display": "flex",
                        "justifyContent": "center",
                    },
                ),
                fac.AntdLayout(
                    [
                        fac.AntdContent(
                            style={"flex": "1", "padding": "20px"},
                            children=[dash.page_container],
                        ),
                        # fac.AntdFooter(
                        #     fac.AntdCenter(
                        #         fac.AntdTitle(
                        #             "页尾示例",
                        #             level=2,
                        #             style={"margin": "0"},
                        #         ),
                        #         style={
                        #             "height": "100%",
                        #         },
                        #     ),
                        #     style={
                        #         "backgroundColor": "rgb(193, 193, 193)",
                        #         "height": "40px",
                        #     },
                        # ),
                    ]
                ),
            ],
            style={"height": "100%"},
        ),
    ],
    style={"height": "100vh"},
)

server = app.server

if __name__ == "__main__":
    app.run(host="0.0.0.0", port="8001", debug=True)